package com.yxt.service;

import java.util.ArrayList;
import java.util.List;

import com.yxt.dao.DBOperator;
import com.yxt.model.Knowledge;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class KnowledgeService {
	private DBOperator dbHelper;

	public KnowledgeService(Context context) {
		dbHelper = new DBOperator(context);
	}

	public void saveKnowledge(Knowledge knowledge) {
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("insert into tab_knowledge (id,name,type,filename,locpath,orderid,server) values (? , ? , ? , ?, ?, ?, ?)",
		new Object[] {knowledge.getId(),knowledge.getName(),knowledge.getType(),knowledge.getFilename(),knowledge.getLocpath(),knowledge.getOrderid(),knowledge.getServer()});
	}
		
	public List<Knowledge> getKnowledgesByType(String type) {
		List<Knowledge> list = new ArrayList<Knowledge>();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select id,name,type,filename,locpath,orderid,server from tab_knowledge where type = ? ",
				new String[] { String.valueOf(type) });
		
		while(cs.moveToNext()) {
			int index = 0;
			Knowledge knowledge = new Knowledge();
			knowledge.setId(cs.getString(index++));
			knowledge.setName(cs.getString(index++));
			knowledge.setType(cs.getString(index++));
			knowledge.setFilename(cs.getString(index++));
			knowledge.setLocpath(cs.getString(index++));
			knowledge.setOrderid(cs.getString(index++));
			knowledge.setServer(cs.getString(index++));
			list.add(knowledge);
		}
		cs.close();
		return list;
	}
	
	public List<Knowledge> getKnowledgeLists() {
		List<Knowledge> list = new ArrayList<Knowledge>();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select id,name,type,filename,locpath,orderid,server from tab_knowledge ",null);		
		while (cs.moveToNext()) {
			int index = 0;
			Knowledge knowledge = new Knowledge();
			knowledge.setId(cs.getString(index++));
			knowledge.setName(cs.getString(index++));
			knowledge.setType(cs.getString(index++));
			knowledge.setFilename(cs.getString(index++));
			knowledge.setLocpath(cs.getString(index++));
			knowledge.setOrderid(cs.getString(index++));
			knowledge.setServer(cs.getString(index++));
			list.add(knowledge);
		}
		cs.close();
		return list;
	}

	public List<String> getKnowledgeType() {
		List<String> types = new ArrayList<String>();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select distinct type from tab_knowledge ",null);		
		while (cs.moveToNext()) {
			int index = 0;
			types.add(cs.getString(index++));
		}
		cs.close();
		return types;
	}
	
	public void deleteKnowledge(){
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("delete from tab_knowledge");
	}
	
	public void closeDB() {
		dbHelper.close();
	}

}
